﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Admin_Role.aspx.cs" 
    Inherits="Xiaov.SmartWeb.Application.SysAdmin.Admin_Role" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <link rel="stylesheet" type="text/css" href="/Resources/xiaovcn/css/ssec-form.css"/>
        <script type="text/javascript" src="/Resources/xiaovcn/scripts/common.js" ></script>
    <script type="text/javascript" src="/Resources/xiaovcn/scripts/usercheck.js"></script>

        <script type="text/javascript">

            function callback(res) {
                if (res.value) {                    
                    $.messager.alert('提示', '操作成功！');
                }
                else {
                    $.messager.alert(res.error);
                }
            }

            function save() {
                var a = $('#t1').val();
                var b = $('#t2').val();
                var c = $('#t3').val();
                var d = $('#t4').attr("checked");
                var e = $('#t5').val();
                var f = getChecked();//权限选择
                Xiaov.SmartWeb.Application.SysAdmin.Admin_Role.SaveRole(a, b, c, d, e,f, callback);
                clearForm();
                $('#add').window('close');
            }

            function getChecked() {
                var val = [];
                $("input:checkbox[id*='$']:checked").each(function () {
                    val.push($(this).attr('id'));
                });
                return (val.join());
            }

            function del(role) {
                Xiaov.SmartWeb.Application.SysAdmin.Admin_Role.DeleteRole(role.ROLEID, callback);
                rebindgrid();
            }

            function rebindgrid()
            {
                //$('#tt').datagrid('reload');
                bindgrid();
                $('#add').window('close');
            }

            function bindgrid() {
                $('#tt').datagrid({
                    title: '角色列表',
                    url: 'Json/Role.aspx',
                    iconCls: 'icon-applicationgo',
                    nowrap: true,
                    rownumbers: true,
                    animate: false,
                    collapsible: false,
                    singleSelect: true,
                    remoteSort:false,
                    columns: [[
                             { field: 'ROLEID', title: '角色ID', sortable: true, width: 100, formatter: function (val, rec) { return unescape(val) } },
                             { field: 'ROLENAME', title: '角色名称', sortable: true, width: 100, formatter: function (val, rec) { return unescape(val) } },
                             { field: 'ROLECOMMENT', title: '备注', width: 100, formatter: function (val, rec) { return unescape(val) } },
                             { field: 'DISABLED', title: '禁用', width: 50, formatter: function (val, rec) { return unescape(val) } },                             
                             { field: 'ROLESORT', title: '排序', width: 50, sortable: true, formatter: function (val, rec) { return unescape(val) } }
                             
                    ]],
                    toolbar: [{
                        text: '增加角色',
                        iconCls: 'icon-add',
                        handler: function () {
                            //增加用户
                            clearForm();
                            bindtree();
                            $('#add').window('open');

                        }
                    }, '-', {
                        text: '修改角色/授权',
                        iconCls: 'icon-edit',
                        handler: function () {
                            editrow();
                        }
                    }, '-', {
                        text: '删除角色',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            //删除用户
                            var role = $('#tt').datagrid('getSelected');
                            if (role) {
                                $.messager.confirm('提示', '确定要删除【' + unescape(role.ROLENAME) + '】这个角色吗?', function (r) {
                                    if (r) {
                                        del(role);
                                        rebindgrid();
                                    }                                    
                                });                                
                            }
                            else
                                alert('请选中一个角色');
                        }
                    }],
                    onLoadSuccess: function () {
                        $('#mask').fadeOut();
                    },
                    onDblClickRow: function () {
                        editrow();
                    }
                });
            }

            function editrow() {
                $('#tab').tabs('select', 1);
                //编辑用户
                var role = $('#tt').datagrid('getSelected');
                if (role) {
                    $('#add').window('open');
                    $('#t1').attr('value', role.ROLEID);
                    $('#t2').attr('value', role.ROLENAME);
                    $('#t3').attr('value', role.ROLECOMMENT);
                    if (role.DISABLED == "1")
                        $('#t4').attr('checked', true);
                    else
                        $('#t4').attr('checked', false);

                    $('#t5').attr('value', role.ROLESORT);
                    //填充checkbox
                    bindtree();
                }
                else {
                    alert('请选中一个角色');
                }
            }

            $(function () {
                bindgrid();              
            });

            function bindtree()
            {
                var roleid = $('#t1').val();
                $('#mtree').treegrid({
                    title: '模块授权',
                    url: 'Json/Module.aspx?t=treegrid&roleid=' + roleid,
                    iconCls: 'icon-edit',
                    nowrap: true,
                    rownumbers: true,
                    animate: false,
                    border: false,                    
                    collapsible: false,
                    singleSelect: true,
                    remoteSort: false,
                    idField: 'MODULEID',
                    treeField: 'MODULENAME',
                    columns: [[
                             { field: 'MODULENAME', title: '名称', sortable: true, width: 260, formatter: function (val, rec) { return unescape(val) } },
                              
                             {
                                 field: 'ACTIONSTRING', title: '权限', width: 800, rowspan: 2, formatter: function (val, rec) {
                                        var rtn = "";                                                                          
                                         var arr = unescape(rec.ACTIONSTRING).split(',');
                                         var prr = unescape(rec.ACTIONAllowString).split(',');
                                         if (arr.length > 0) {
                                             for (var i = 0; i < arr.length; i++) {
                                                 //获取角色的 权限值
                                                 var pm = "";
                                                 if (roleid) {
                                                     //var flag = Xiaov.SmartWeb.Application.SysAdmin.Admin_Role.Check(rec.MODULEID, roleid, i);
                                                     //判断i 是否在prr                                                     
                                                     if(in_array(i,prr)&&rec.ACTIONAllowString!="")
                                                         pm = "checked='checked' style='background-color:green;'";
                                                     }
                                                     else
                                                         pm = "";  
                                                 rtn += "<input id='" + rec.MODULEID + "$" + i + "' type='checkbox' value='" + i + "'" + pm + "/>" + arr[i] + "&nbsp;&nbsp;&nbsp;";
                                             }
                                         }
                                         return rtn;
                                         return unescape(rec.ACTIONAllowString);
                                 }
                             }
                    ]],
                    onLoadSuccess: function () {
                        $('#mask').fadeOut();
                    }
                });               

            }

            function in_array(str, arr) {
                // 得到needle的类型
                var type = typeof(str);

                if (type == "string" || type == "number") {
                    for (var i in arr) {
                        if (arr[i] == str) {
                            return true;
                        }
                    }
                }
                return false;
            }

           </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">   

     <table id="tt">
    </table>      
    <script>
        $('#tt').css('height', userScreenHeight + 20);
    </script>
    <div id="add" class="easyui-window" closed="true" modal="true" maximized="true" title="角色信息" style="width:600px;">
        <div class="easyui-tabs" id="tab">
            <div title="基本信息" style="padding:10px">
                <div id="contents" class="ssec-form">        
        <table>
         <tr>
                <td><div class="ssec-label">角色ID：</div></td>
                <td><div class="ssec-text normal"><asp:TextBox ID="t1" ClientIDMode="Static" runat="server" CssClass="ssec-text-field " Enabled="false"></asp:TextBox></div></td>             
           </tr> 
        <tr>
                <td><div class="ssec-label">角色名称：</div></td>
                <td><div class="ssec-text normal"><asp:TextBox ID="t2" ClientIDMode="Static" runat="server" CssClass="ssec-text-field"></asp:TextBox></div></td>             
           </tr> 
        <tr>
                <td><div class="ssec-label">备注：</div></td>
                <td><div class="ssec-text normal"><asp:TextBox ID="t3" ClientIDMode="Static" runat="server" CssClass="ssec-text-field"></asp:TextBox></div></td>             
        </tr>         
        <tr>
                <td><div class="ssec-label">禁用：</div></td>
                <td><asp:Checkbox ID="t4" ClientIDMode="Static" runat="server"></asp:Checkbox></td>             
        </tr>
        <tr>
                <td><div class="ssec-label">排序：</div></td>
                <td><div class="ssec-text normal"><asp:TextBox ID="t5" ClientIDMode="Static" runat="server" CssClass="ssec-text-field easyui-numberbox"></asp:TextBox></div></td>             
        </tr> 
            </table>       
     </div>
            </div>
            <div title="角色授权" style="padding:0px">
              <table id="mtree"></table>               
           </div>            
            </div>
        <div style="padding:5px; text-align:right;" ><a  class="easyui-linkbutton"  onclick="if(confirm('确定要保存角色权限吗？')){save();}" iconCls="icon-save">保存</a></div> 
    </div>
           
        
</asp:Content>
